﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileMonitor.Service.Tracking.Notify
{
    public enum NotifyType
    {
        Email,
        OC,
        Message
    }
    public class NotifyService
    {
        private static Dictionary<NotifyType, INotify> _instanceCache;

        static NotifyService()
        {
            _instanceCache = new Dictionary<NotifyType, INotify>();
        }

        private static object lockObj = new object();
        public static void EnNotifyTask(NotifyTask task)
        {
            INotify _instance=null;
            if (!_instanceCache.ContainsKey(task.NotifyType))
            {
                lock (lockObj)
                {
                    if (!_instanceCache.ContainsKey(task.NotifyType))
                    {
                        _instance = CreateNotifyInstance(task.NotifyType);
                        _instance.StatNotify();
                        _instanceCache.Add(task.NotifyType, _instance);
                    }
                }
            }
            _instance = _instanceCache[task.NotifyType];
            if (_instance != null)
            {
                _instance.Notify(task);
            }
          
        }



        private static INotify CreateNotifyInstance(NotifyType type)
        {
            switch (type) 
            {
                case NotifyType.Email:
                    return new EmailNotify();
                case NotifyType.Message:
                    throw new NotImplementedException();
                case NotifyType.OC:
                    throw new NotImplementedException();
                default:
                    throw new NotImplementedException();

            }
        }

        public static void StopNotify()
        {
            foreach (INotify instance in _instanceCache.Values)
            {
                instance.StopNotify();
            }
        }
    }
}
